/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fix.it.source.fixit;





import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.faces.bean.ManagedBean;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
 *
 * @author Aleksandra
 */

@Stateless
public class ProblemBean implements Serializable{
    
    private static final long serialVersionUID = 1L;
   
        @PersistenceContext(unitName = "fix.it.source_FixIt_war_1.0-SNAPSHOTPU")
        private EntityManager em;
        
        private List<Problem> problemList;
        private Problem selectedProblem;
         
        
  
    
        public void addProblem(Problem p){
        
            em.persist(p);
        }
        public  List<Problem> getProblemList() {
        
           Query q=em.createQuery("SELECT p FROM Problem p");
          problemList=q.getResultList();
          
                return problemList;
        }

        public Problem getSelectedProblem() {  
            return selectedProblem;  
         }  
        
  
        public void setSelectedProblem(Problem selectedProblem) {  
            this.selectedProblem = selectedProblem;  
        }
    
        
        public  Problem getProblem(long i){
             Query q = em.createQuery("SELECT p FROM Problem p WHERE p.id = " + i);
             /* if we choose to show all the data related to the problem even the ones that are foreignk keys */
             /* SELECT p FROM Problem p join PROBLEMTYPE pt on p.PROBLEMTYPEID = pt.ID WHERE p.id = " + i */
             Problem p = (Problem)q.getResultList().get(0);
             return p;
        }
	
	public  int indexOf(Problem problem) {
		// TODO Auto-generated method stub
		return problemList.indexOf(problem);
	}

    
}
